package com.ksd.modules.rbac.api.controller;

import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.modules.rbac.api.entity.SysApis;
import com.ksd.modules.rbac.api.po.SysApisSavePO;
import com.ksd.modules.rbac.api.po.SysApisSearchPO;
import com.ksd.modules.rbac.api.service.ISysApisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@RestController
public class SysApisController  extends BaseController {

    @Autowired
    private ISysApisService sysApisService;

    /**
     * 查询首页有系统日志接口
     *
     * @return
     */
    @PostMapping("/sysapis/root")
    public R<List<SysApis>> findApisRoot() {
        return R.data(sysApisService.findApisRoot());
    }

    /**
     * 查询首页有系统日志接口
     *
     * @return
     */
    @PostMapping("/sysapis/list")
    public R<List<SysApis>> findSysApis(@Validated @RequestBody SysApisSearchPO sysApissSearchPO) {
        return R.data(sysApisService.findApis(sysApissSearchPO));
    }

    /**
     * 保存和更新
     *
     * @param adminSysApisSavePO
     * @return
     */
    @PostMapping("/sysapis/saveupdate")
    @ApiLog("菜单保存和更新")
    public R<Boolean> saveUpdateSysApis(@Validated @RequestBody SysApisSavePO adminSysApisSavePO) {
        return R.data(sysApisService.saveUpdateSysApis(adminSysApisSavePO));
    }

    /**
     * 根据id查询菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/sysapis/get/{id}")
    public R<SysApis> getSysApisById(@PathVariable("id") Long id) {
        return R.data(sysApisService.getSysApisById(id));
    }

    /**
     * 根据id删除菜单
     *
     * @param id
     * @return
     */
    @PostMapping("/sysapis/del/{id}")
    @ApiLog("菜单保存和更新")
    public R<Boolean> delSysApisById(@PathVariable("id") Long id) {
        return R.data(sysApisService.deleteSysApisById(id));
    }

    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/sysapis/status/{id}/{field}/{value}")
    @ApiLog("菜单修改状态")
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(sysApisService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(sysApisService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }
}
